<?php
/*pc端客服*/
namespace app\mobile\controller;

use app\api\model\Client;
use app\api\model\Users;
use think\Session;

class Kf extends Common
{
    public function _initialize()
    {
        parent::_initialize();

    }

    public function index()
    {
        if(!$this->check_login() || !$this->CurrentUserIsKf())
        {

            $this->redirect('login');
        }

    }



    public function login()
    {
        $this->assign([
            'randToken' => $this->createRandSession()
        ]);
        return $this->fetch();
    }

    //客服登录接口
    public function kfLogin()
    {
        if(!$this->chkRandSession(input('post.randToken')))
        {
            $this->error('令牌错误');
        }
        $kf = [
            'kf_username' => input('post.username'),
            'kf_password' => input('post.password')
        ];
        $KfModel = new \app\api\model\Kf($this->modelDefaultParam());
        if(!($user = $KfModel->login($kf)))
        {
            $this->error('登录信息错误');
        }
        else
        {
            $this->setCurrentUserMsg($user);
            $this->success('登录成功',url('Index/index'));
        }
    }

    public function addkf()
    {
        $kfModel = new \app\api\model\Kf();
        $data = $kfModel->addKf(['kf_username'=>'dd','kf_password'=>'123456','nickname'=>'dd']);
        print_r($data);
    }

    //获取所有客服
    public function getList()
    {
        /**
         * 参数说明
         * post.client_id  当前请求的client_id 只能是客服id并且该操作者是在线状态的才能获取客服列表
         */
        $client_id = input('post.client_id');
        $client = new Client($this->modelDefaultParam());
        $KfModel = new \app\api\model\Kf($this->modelDefaultParam());

        //获取所有在线的客户端id
        $allKfClientId = $client->getAllOnLineClientId();

        //当前客服不在线
        if(!(in_array($client_id,$allKfClientId) && $this->isKf($client_id)))
        {
            return ['error'=>1,"msg"=>"您没有权限,或者该客服不在线,不能进行切换噢"];
        }

        //给该客服发送 所有客服，以及是否在线状态
        $client->sendOneUser(['type'=>'kfList','msg'=>$KfModel->getAllKf($allKfClientId)],$client_id);
        return ['error'=>0,"msg"=>"请求成功"];

    }

    //切换客服操作
    public function changeKf()
    {

        /**
         * 参数说明
         * post $client_id 为当前转换的操作者客服
         * new_client_id 转换到新的客服
         * to_client_id  给哪位用户转换
         */

        $client = new Client($this->modelDefaultParam());
        $userModel = new Users($this->modelDefaultParam());
        //客服id
        $client_id = input('post.client_id');
        //新客服id
        $new_client_id = input('post.new_client_id');
        //用户id
        $to_client_id = input('post.to_client_id');

        if($client_id == $new_client_id || $client_id == $to_client_id || $new_client_id == $to_client_id) return ['error'=>1,"msg"=>"非法操作"];

        //当前切换操作者或者切换的客服id也不是客服
        if(!$this->isKf($client_id) && !$this->isKf($new_client_id)) return ['error'=>1,"msg"=>"非法操作"];

        //用户的信息
        $userMsg = $client->getSessionByUserid($to_client_id);
        //旧客服信息
        $oldKfMsg = $client->getSessionByUserid($client_id);
        //新客服信息
        $newKfrMsg = $client->getSessionByUserid($new_client_id);

        //用户是否存在
        if(empty($userMsg))  return ['error'=>1,"msg"=>"不存在该用户"];
        if(empty($newKfrMsg))  return ['error'=>1,"msg"=>"不存在客服"];

        //给新客服发送新用户信息
        $client->sendOneUser(
            ['type'=>'text','msg'=>base64_encode($oldKfMsg['user_name']."客服向您切换了当前用户。"),'client_id'=>$to_client_id,'client_name'=>$userMsg['user_name'],'isSys'=>1,'msgid'=>"",'is_html_out'=>1]
            ,$new_client_id
        );
        //给用户发送被切换的消息
        $client->sendOneUser(
            ['type'=>'changeKf','msg'=>['nickname'=>$newKfrMsg['user_name'],'client_id'=>$to_client_id,'new_client_id'=>$new_client_id,'old_client_id'=>$client_id]]
            ,$to_client_id
        );

        return ['error'=>0,"msg"=>"切换成功"];
    }

    //生成随机的书写入session中
    public function createRandSession()
    {
        $token = randString(30);
        session('randToken',$token);
        return $token;
    }

    //验证随机数
    public function chkRandSession($token)
    {
        return $token == session('randToken');
    }


}